package com.ylkj.patient.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.yilijk.base.AppBase;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.BitmapUtils;
import com.ylkj.patient.BuildConfig;
import com.ylkj.patient.MainApplication;
import com.ylkj.patient.R;
import com.ylkj.patient.wxapi.Util;

import org.jetbrains.annotations.NotNull;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/11/24
 */
public class WXShareUtils {
    private static int mTargetScene = SendMessageToWX.Req.WXSceneSession;
    private static final int THUMB_SIZE = 150;


    /**
     * 分享文本
     * @param openId
     * @param text
     */

    public static void wxShareTxt(String openId,String text){
        /**
         * text
         * openid
         */
//初始化一个 WXTextObject 对象，填写分享的文本内容
        WXTextObject textObj = new WXTextObject();
        textObj.text = text;

//用 WXTextObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.description = text;

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("text");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;
//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }


    /**
     * 微信分享网络图片
     * @param openId
     * @param imgUrl
     */
    public static void wxShareUrlImage(String openId,String imgUrl){
        Bitmap bmp = BitmapUtils.GetImageInputStream(imgUrl);

//初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;
//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }

    /**
     * 微信分享本地图片
     * @param openId
     * @param imgUrl
     */
    public static void wxShareLocalImage(String openId,String imgUrl){
        /**
         * imgUrl
         * openId
         */
        Bitmap bmp = BitmapUtils.openImage(imgUrl);
//初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;
//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }


    /**
     *
     */
    /**
     * 微信分享base64图片
     * @param openId
     * @param baseimg
     */
    public static void wxShareBase64Image(String openId,String baseimg){
        /**
         * imgUrl
         * openId
         */
        Bitmap bmp = BitmapUtils.base64ToBitmap(baseimg);
//初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;
//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }
    /**
     * 微信分享视频
     * @param openId
     * @param videoUrl
     * @param videoTitle
     * @param videoDescription
     */
    public static void wxShareVideo(String openId,String videoUrl,String videoTitle,String videoDescription){

//初始化一个WXVideoObject，填写url
        WXVideoObject video = new WXVideoObject();
        video.videoUrl = videoUrl;
//用 WXVideoObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(video);
        msg.title = videoTitle;
        msg.description = videoDescription;

        Bitmap thumbBmp = BitmapFactory.decodeResource(AppBase.getApplication().getResources(), R.mipmap.app_logo);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("video");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;

//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }

    /**
     * 微信分享H5
     * @param openId
     * @param webUrl
     * @param webTitle
     * @param webDescription
     */
    public static void wxShareH5(String openId,String webUrl,String webTitle,String webDescription){
        //初始化一个WXWebpageObject，填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = webUrl;

//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = webTitle;
        msg.description = webDescription;
        Bitmap thumbBmp = BitmapFactory.decodeResource(AppBase.getApplication().getResources(), R.mipmap.app_logo);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = mTargetScene;
        req.userOpenId = openId;

//调用api接口，发送数据到微信
        MainApplication.iwxapi.sendReq(req);
    }

    /**
     * 微信分享小程序
     * @param webpageUrl  兼容低版本的网页链接
     * @param userName   小程序原始id
     * @param path    小程序页面路径
     * @param title   小程序消息title
     * @param description  小程序消息desc
     * @param thumbBmp  小程序消息封面图片 小于128k 默认为LOGO
     */
    public static void wxShareUniApp(@NotNull String webpageUrl, @NotNull String userName, @NotNull String path, @NotNull String title, @NotNull String description, @NotNull String thumbBmp){
        ALog.e("WXShareUtils","wxShareUniApp");
        WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
//        miniProgramObj.webpageUrl = webpageUrl; // 兼容低版本的网页链接
        miniProgramObj.webpageUrl = "http://www.yilijk.com"; // 兼容低版本的网页链接
        miniProgramObj.miniprogramType = BuildConfig.DOMIN_NO == 4 ? WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE :// 正式版
                                            BuildConfig.DOMIN_NO == 3 ? WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW :// 测试版
                                                    WXMiniProgramObject.MINIPROGRAM_TYPE_TEST;// 体验版
        miniProgramObj.userName = userName;     // 小程序原始id
        miniProgramObj.path = path;            //小程序页面路径；对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"
        WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
        msg.title = title;                    // 小程序消息title
        msg.description = description;               // 小程序消息desc
        if (!TextUtils.isEmpty(thumbBmp)){
            Bitmap bitmap = BitmapUtils.GetImageInputStream(thumbBmp);
            ALog.e(bitmap.getByteCount()+"byte");
            byte[] bytes = BitmapUtils.bmpToByteArray(bitmap, 128 * 1024, true);
            ALog.e(bytes.length+"byte");
            msg.thumbData = bytes;
        }else{
            Bitmap bitmap = BitmapFactory.decodeResource(AppBase.getApplication().getResources(), R.drawable.icon_default_square);
            msg.thumbData = Util.bmpToByteArray(bitmap, true);// 小程序消息封面图片，小于128k
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("miniProgram");
        req.message = msg;
        req.scene = mTargetScene;  // 目前只支持会话
        MainApplication.iwxapi.sendReq(req);
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

}
